<!DOCTYPE html>
<html>
<body>
<p>This tests changing the focus on keypress event. The character typed by user should be inserted into input. This test only runs in DRT.</p>
<pre><script>

function runTest(name, shouldChangeSelection) {
    var dummy = document.createElement('input');
    document.body.appendChild(dummy);

    var element = document.createElement(name);
    document.body.appendChild(element);
    element.addEventListener('keypress', function() {
        if (element.value.length >= 2)
            dummy.focus();
    })

    document.writeln(name + (shouldChangeSelection ? ' with selectionchange' : ' without selectionchange'));

    element.focus();
    eventSender.keyDown('a');
    eventSender.leapForward(100);
    eventSender.keyDown(name == 'textarea' ? 'Enter' : 'b');
    eventSender.leapForward(100);
    if (shouldChangeSelection) {
        element.selectionStart = 1;
        element.selectionEnd = 1;
    }
    eventSender.keyDown('c');
    eventSender.leapForward(100);

    var expected = null;
    if (name == 'textarea')
        expected = shouldChangeSelection ? 'ac\n' : 'a\nc';
    else
        expected = shouldChangeSelection ? 'acb' : 'abc';

    if (element.value == expected)
        document.writeln('PASS');
    else
        document.writeln('FAIL: value was "' + element.value + '" but expected "' + expected + '"');
    document.writeln();
}

if (window.testRunner && window.eventSender) {
    testRunner.dumpAsText();

    runTest('input', false);
    runTest('input', true);
    runTest('textarea', false);
    runTest('textarea', true);

    document.writeln('DONE');
}

</script></pre>
</body>
</html>
